package zzs.generalframework.console.core.user.userattence.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.user.userattence.UserAttenceBO;
import zzs.generalframework.common.core.user.userattence.UserAttenceInfo;
import zzs.generalframework.common.core.user.userattence.UserAttencePO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.user.userattence.dao.IUserAttenceDao;
import zzs.generalframework.console.core.user.userattence.service.IUserAttenceService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.user.userattence.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.user.userattence.service.impl.userAttenceServiceImpl")
public class UserAttenceServiceImpl implements IUserAttenceService
{

	private Logger logger = LoggerFactory.getLogger(UserAttenceServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IUserAttenceDao userAttenceDao;

	@Override
	public ConsoleReturnBO saveUserAttence(UserAttenceBO userAttenceBO)
	{
		logger.debug("====保存用户考勤信息开始====");
		logger.debug("====保存用户考勤信息.入参:UserAttenceBO=[{}]====", userAttenceBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserAttencePO userAttencePO = new UserAttencePO();
		copyUserAttenceBO2UserAttencePO(userAttenceBO, userAttencePO);
		Boolean insertResult = userAttenceDao.insert(userAttencePO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存用户考勤信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户考勤信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveUserAttence(List<UserAttenceBO> userAttenceBOList) throws ConsoleException
	{
		logger.info("====保存用户考勤信息列表开始====");
		logger.debug("====保存用户考勤信息列表.入参:UserAttenceBOList=[{}]====", userAttenceBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserAttenceBO itemBO : userAttenceBOList)
		{
			ConsoleReturnBO itemResult = saveUserAttence(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存用户考勤信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户考勤信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getUserAttence(UserAttenceInfo userAttenceInfo)
	{
		logger.info("====获取用户考勤信息开始====");
		logger.debug("====获取用户考勤信息.入参:UserAttenceInfo=[{}]====", userAttenceInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserAttencePO> userAttencePOList = userAttenceDao.select(userAttenceInfo);
		if (userAttencePOList == null || userAttencePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (userAttencePOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			UserAttenceBO userAttenceBO = new UserAttenceBO();
			copyUserAttencePO2UserAttenceBO(userAttencePOList.get(0), userAttenceBO);
			returnConsoleReturnBO.setReturnObj(userAttenceBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			UserAttenceBO userAttenceBO = new UserAttenceBO();
			copyUserAttencePO2UserAttenceBO(userAttencePOList.get(0), userAttenceBO);
			returnConsoleReturnBO.setReturnObj(userAttenceBO);
		}
		logger.debug("====获取用户考勤信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户考勤信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listUserAttence(UserAttenceInfo userAttenceInfo)
	{
		logger.info("====获取用户考勤信息列表开始====");
		logger.debug("====获取用户考勤信息列表.入参:UserAttenceInfo=[{}]====", userAttenceInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserAttencePO> userAttencePOList = userAttenceDao.select(userAttenceInfo);
		if (userAttencePOList == null || userAttencePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<UserAttenceBO> resultList = new ArrayList<UserAttenceBO>();
			for (UserAttencePO itemPO : userAttencePOList)
			{
				UserAttenceBO itemBO = new UserAttenceBO();
				copyUserAttencePO2UserAttenceBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取用户考勤信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户考勤信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserAttence(UserAttenceBO userAttenceBO)
	{
		logger.info("====更新用户考勤信息开始====");
		logger.debug("====更新用户考勤信息.入参:UserAttenceBO=[{}]====", userAttenceBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserAttencePO userAttencePO = new UserAttencePO();
		copyUserAttenceBO2UserAttencePO(userAttenceBO, userAttencePO);
		Boolean updateResult = userAttenceDao.update(userAttencePO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新用户考勤信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户考勤信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserAttence(List<UserAttenceBO> userAttenceBOList) throws ConsoleException
	{
		logger.info("====更新用户考勤信息列表开始====");
		logger.debug("====更新用户考勤信息列表.入参:List<UserAttenceBO>=[{}]====", userAttenceBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserAttenceBO itemBO : userAttenceBOList)
		{
			ConsoleReturnBO itemResult = updateUserAttence(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====更新用户考勤信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户考勤信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserAttence(UserAttenceBO userAttenceBO)
	{
		logger.info("====删除用户考勤信息开始====");
		logger.debug("====删除用户考勤信息.入参:UserAttenceBO＝[{}]======", userAttenceBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserAttencePO userAttencePO = new UserAttencePO();
		copyUserAttenceBO2UserAttencePO(userAttenceBO, userAttencePO);
		Boolean deleteResult = userAttenceDao.delete(userAttencePO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除用户考勤信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户考勤信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserAttence(List<UserAttenceBO> userAttenceBOList) throws ConsoleException
	{
		logger.info("====删除用户考勤信息列表开始====");
		logger.debug("====删除用户考勤信息列表.入参:List<UserAttenceBO>＝[{}]======", userAttenceBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserAttenceBO itemBO : userAttenceBOList)
		{
			ConsoleReturnBO itemResult = removeUserAttence(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除用户考勤信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户考勤信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countUserAttence(UserAttenceBO userAttenceBO)
	{
		logger.info("====统计用户考勤信息开始====");
		logger.debug("====统计用户考勤信息。入参：UserAttenceBO＝[{}]======", userAttenceBO);
		Long returnLong = new Long(0);
		UserAttencePO userAttencePO = new UserAttencePO();
		copyUserAttenceBO2UserAttencePO(userAttenceBO, userAttencePO);
		returnLong = userAttenceDao.count(userAttencePO);
		logger.debug("====统计用户考勤信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计用户考勤信息结束====");
		return returnLong;
	}

	private void copyUserAttenceBO2UserAttencePO(UserAttenceBO userAttenceBO, UserAttencePO userAttencePO)
	{
		beanUtils.copyBeanNotNull(userAttenceBO, userAttencePO);
		if (userAttenceBO.getSysID() != null)
		{
			userAttencePO.setSeqID(userAttenceBO.getSysID());
		}
	}

	private void copyUserAttencePO2UserAttenceBO(UserAttencePO userAttencePO, UserAttenceBO userAttenceBO)
	{
		beanUtils.copyBeanNotNull(userAttencePO, userAttenceBO);
		if (userAttencePO.getSeqID() != null)
		{
			userAttenceBO.setSysID(userAttencePO.getSeqID());
		}
	}

}
